//
//  CZStatusToolbar.swift
//  CZ29VVeibo
//
//  Created by apple on 16/6/29.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class CZStatusToolbar: UIView {
    var retweetButton: UIButton?
    var commentButton: UIButton?
    var unlikeButton: UIButton?

    // 供外界赋值
    var status: CZStatus?{
        didSet{
            
            retweetButton?.setTitle(status?.repostsStr, forState: UIControlState.Normal)
            commentButton?.setTitle(status?.commentsStr, forState: UIControlState.Normal)
            unlikeButton?.setTitle(status?.attitudesStr, forState: UIControlState.Normal)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    /*
    - 底部toolbar 显示的转发评论赞 格式----业务需求
    - 如果 count <= 0
    - 显示格式： 转发 评论 赞 文字
    - 如果 count > 0 && count < 10000
    - 显示格式: 是多少显示多少 例如 8888  显示 8888
    - 如果 count >= 10000
    - 显示格式: x.x 万  例如 12000  显示 1.2 万
    -  例如 10000  显示 1万  20000  显示 2万 x万
    
    */
    func getCountString(count: Int, title: String) -> String{
        if count <= 0 {
            return title
        }else if count > 0 && count < 10000{
            return "\(count)"
        }else {
            let countF = CGFloat(count / 1000)/10
            var countStr = "\(countF)"
            // containsString --》判断字符串是否包含字符串
            if countStr.containsString(".0"){
                countStr = countStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            
            return "\(countStr)万"
        }
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        // 添加控件
        retweetButton = addChildButtons("timeline_icon_retweet", title: "转发")
        commentButton = addChildButtons("timeline_icon_comment", title: "评论")
        unlikeButton = addChildButtons("timeline_icon_unlike", title: "赞")
        // 分割线
        let line1 = addChildLines()
        let line2 = addChildLines()
        // 添加约束
        retweetButton?.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        commentButton?.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.width.equalTo(unlikeButton!)
        }
        unlikeButton?.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        }
        
        line1.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self)
            make.centerX.equalTo(retweetButton!.snp_trailing)
        }
        line2.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self)
            make.centerX.equalTo(commentButton!.snp_trailing)
        }
        
    }
    
    // 创建button
    private func addChildButtons(imgName: String, title: String) -> UIButton{
        let button = UIButton()
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        // 设置背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        // 添加控件
        addSubview(button)
        return button
    }
    
    private func addChildLines() -> UIImageView{
        let img = UIImageView(imgName: "timeline_card_bottom_line")
        // 添加控件
        addSubview(img)
        return img
    }
    
    
}
